package com.example.tool;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.*;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import androidx.fragment.app.FragmentActivity;

import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;

import java.util.ArrayList;
import java.util.Locale;

import com.example.app.MyApplication;
import com.example.view.LoginActivity;
import com.example.view.R;
public class BaseActivity extends FragmentActivity implements OnClickListener, OnScrollListener {

	public static Context context;

	protected static String sdkUrl;

	public boolean mHandleProgressExternal = false;

	public View mProgressView;

	public LinearLayout progressBar = null;

	public LinearLayout notice = null;

	public TextView title;

	public ImageButton btn_back;

	protected static ArrayList<Activity> ActivityManager = new ArrayList<Activity>();

	public Handler mHandler = new Handler();

	public SharedPreferences share;

	private Animation animShow;
	private Animation animHide;

	public static BaseActivity instance;
	private TextView tv_notice;

	private void initAnim() {   //����
		animShow = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
				Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -1);
		animShow.setDuration(1000);

		animHide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
				Animation.RELATIVE_TO_SELF, -1, Animation.RELATIVE_TO_SELF, 0);
		animHide.setDuration(1000);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActivityManager.add(this);
		instance = this;
		initAnim();
		context = this;
		share = getSharedPreferences("config", MODE_PRIVATE);
	}

	boolean flag = true;
	public void showProgressBar() {
		runOnUiThread(new Runnable() {
			public void run() {
				if (progressBar == null) {
					progressBar = createProgressBar(BaseActivity.this, null);
				}
				progressBar.setVisibility(View.VISIBLE);
			}
		});
	}
	public void showProgressBar(final String text) {
		runOnUiThread(new Runnable() {
			public void run() {
				if (progressBar == null) {
					progressBar = createProgressBar(BaseActivity.this, null);
				}
				tv_pb.setText(text);
				progressBar.setVisibility(View.VISIBLE);
			}
		});
	}
	@SuppressLint("InflateParams")
	public LinearLayout createProgressBar(Activity activity, Drawable customIndeterminateDrawable) {
		FrameLayout rootContainer = (FrameLayout) activity.findViewById(android.R.id.content);
		FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		lp.gravity = Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL;

		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.pb, null);
		progressBar = (LinearLayout) view.findViewById(R.id.result);
		progressBar.setLayoutParams(lp);
		tv_pb = (TextView) view.findViewById(R.id.result_tv);
		rootContainer.addView(view);
		return progressBar;
	}
	public void hideProgressBar() {
		runOnUiThread(new Runnable() {
			public void run() {
				if (progressBar == null) {
					progressBar = createProgressBar(BaseActivity.this, null);
				}
				progressBar.setVisibility(View.GONE);
			}
		});
	}


	@Override
	protected void onStart() {
		super.onStart();
	}
	public void startActivity(Intent intent) {
		super.startActivity(intent);
	}

	public boolean needLogin() {
		SharedPreferences sharedPreferences = getSharedPreferences("test", Context.MODE_PRIVATE);
		String phone = sharedPreferences.getString("token", "");
		if (phone.equals("")) {
			MyApplication.isLogin = true;
			Intent intent = new Intent(this, LoginActivity.class);
			super.startActivity(intent);
			return true;
		} else {
			return false;
		}
	}

	public boolean needLogin2() {
		SharedPreferences sharedPreferences = getSharedPreferences("test", Context.MODE_PRIVATE);
		String phone = sharedPreferences.getString("token", "");
		if (phone.equals("")) {
			return true;
		} else {
			return false;
		}
	}
	@Override
	public void finish() {
		super.finish();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	public void showToast(final String text) {
		runOnUiThread(new Runnable() {
			public void run() {
				Toast toastCustom = new Toast(context);
				TextView tv = new TextView(context);
				tv.setText(text);
				tv.setTextSize(20f);
				tv.setPadding(10, 2, 10, 2);
				tv.setGravity(Gravity.CENTER);
				tv.setTextColor(context.getResources().getColor(R.color.white));
				tv.setBackgroundResource(R.drawable.bg_toast);
				toastCustom.setView(tv);
				toastCustom.setDuration(Toast.LENGTH_SHORT);
				toastCustom.setGravity(Gravity.CENTER, 0, 0);
				toastCustom.show();
			}
		});
	}
	public void showToast(final int id) {
		runOnUiThread(new Runnable() {
			public void run() {
				String text = getString(id);
				Toast toastCustom = new Toast(context);
				TextView tv = new TextView(context);
				tv.setText(text);
				tv.setTextSize(20f);
				tv.setPadding(10, 2, 10, 2);
				tv.setGravity(Gravity.CENTER);
				tv.setTextColor(context.getResources().getColor(R.color.white));
				tv.setBackgroundResource(R.drawable.bg_toast);
				toastCustom.setView(tv);
				toastCustom.setDuration(Toast.LENGTH_SHORT);
				toastCustom.setGravity(Gravity.CENTER, 0, 0);
				toastCustom.show();
			}
		});
	}

	public void initHeadBar(int backShow, int titleShow, String titleText) {
		if (backShow == View.VISIBLE) {
			btn_back.setOnClickListener(this);
			btn_back.setVisibility(backShow);
		}
		if (titleShow == View.VISIBLE) {
			title.setText(titleText);
			title.setVisibility(titleShow);
		}
	}


	public void updateActivity(String sta, Class<?> activity) {
		Locale myLocale = new Locale(sta);
		Resources res = getResources();
		DisplayMetrics dm = res.getDisplayMetrics();
		Configuration conf = res.getConfiguration();
		conf.locale = myLocale;
		res.updateConfiguration(conf, dm);
	}

	public float[] getViewSize(View view) {

		return null;
	}


	protected void exit() {
		for (Activity ativity : ActivityManager) {
			ativity.finish();
		}
	}

	class HomeKeyEventBroadCastReceiver extends BroadcastReceiver {
		static final String SYSTEM_REASON = "reason";
		static final String SYSTEM_HOME_KEY = "homekey";// home key
		static final String SYSTEM_RECENT_APPS = "recentapps";// long home key

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
				String reason = intent.getStringExtra(SYSTEM_REASON);
				if (reason != null) {
					if (reason.equals(SYSTEM_HOME_KEY)) {
					} else if (reason.equals(SYSTEM_RECENT_APPS)) {
					}
				}
			}
		}
	}



	public void finishAll() {
		for (int i = 0; i < ActivityManager.size(); i++) {
			ActivityManager.get(i).finish();
		}
	}

	protected void updateToNewVersion() {

	}

	@Override
	public void onClick(View v) {
	}

	public TextView tv_pb;




	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {

	}
}
